<template>
  <div class="workforce-container">
    <h2>护士排班</h2>
    
    <!-- 添加按钮 -->
    <el-button type="primary" @click="showAddDialog">添加</el-button>
    
    <!-- 数据表格 -->
    <el-table :data="staffList" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="group" label="护理单元" width="120" />
      <el-table-column prop="nurse" label="护士" width="120" />
      <el-table-column prop="trainee" label="分管实习生" width="120" />
      <el-table-column prop="supervisor" label="监管实习生" width="120" />
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      :title="dialogType === 'add' ? '添加人员' : '编辑人员'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="formData" ref="formRef" :rules="rules" label-width="100px">
        <el-form-item label="护理单元" prop="group">
          <el-input v-model="formData.group" placeholder="请输入护理单元" />
        </el-form-item>
        <el-form-item label="护士" prop="nurse">
          <el-select v-model="formData.nurse" placeholder="请选择护士">
            <el-option
              v-for="nurse in nurseOptions"
              :key="nurse"
              :label="nurse"
              :value="nurse"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="分管实习生" prop="trainee">
          <el-select v-model="formData.trainee" placeholder="请选择分管实习生">
            <el-option
              v-for="trainee in traineeOptions"
              :key="trainee"
              :label="trainee"
              :value="trainee"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="监管实习生" prop="supervisor">
          <el-select v-model="formData.supervisor" placeholder="请选择监管实习生">
            <el-option
              v-for="supervisor in supervisorOptions"
              :key="supervisor"
              :label="supervisor"
              :value="supervisor"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  name: 'Workforce',
  setup() {
    // 表格数据
    const staffList = ref([
      {
        id: 1,
        group: '一组',
        nurse: '张三',
        trainee: '王五',
        supervisor: '赵六'
      },
      {
        id: 2,
        group: '二组',
        nurse: '李四',
        trainee: '小红',
        supervisor: '小李'
      }
    ])

    // 表单数据
    const formData = reactive({
      id: '',
      group: '',
      nurse: '',
      trainee: '',
      supervisor: ''
    })

    // 对话框控制
    const dialogVisible = ref(false)
    const dialogType = ref('add')

    // 选项数据
    const nurseOptions = ['张三', '李四', '王五']
    const traineeOptions = ['王五', '小红', '小明']
    const supervisorOptions = ['赵六', '小李', '小王']

    // 表单验证规则
    const rules = {
      group: [{ required: true, message: '请输入护理单元', trigger: 'blur' }],
      nurse: [{ required: true, message: '请选择护士', trigger: 'change' }],
      trainee: [{ required: true, message: '请选择分管实习生', trigger: 'change' }],
      supervisor: [{ required: true, message: '请选择监管实习生', trigger: 'change' }]
    }

    // 显示添加对话框
    const showAddDialog = () => {
      dialogType.value = 'add'
      Object.keys(formData).forEach(key => {
        formData[key] = ''
      })
      dialogVisible.value = true
    }

    // 处理编辑
    const handleEdit = (row) => {
      dialogType.value = 'edit'
      Object.keys(formData).forEach(key => {
        formData[key] = row[key]
      })
      dialogVisible.value = true
    }

    // 处理删除
    const handleDelete = (row) => {
      ElMessageBox.confirm('确认删除该记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const index = staffList.value.findIndex(item => item.id === row.id)
        if (index !== -1) {
          staffList.value.splice(index, 1)
          ElMessage.success('删除成功')
        }
      }).catch(() => {})
    }

    // 处理提交
    const handleSubmit = () => {
      if (dialogType.value === 'add') {
        const newId = staffList.value.length > 0 
          ? Math.max(...staffList.value.map(item => item.id)) + 1 
          : 1
        staffList.value.push({
          id: newId,
          group: formData.group,
          nurse: formData.nurse,
          trainee: formData.trainee,
          supervisor: formData.supervisor
        })
        ElMessage.success('添加成功')
      } else {
        const index = staffList.value.findIndex(item => item.id === formData.id)
        if (index !== -1) {
          staffList.value[index] = { ...formData }
          ElMessage.success('编辑成功')
        }
      }
      dialogVisible.value = false
    }

    return {
      staffList,
      formData,
      dialogVisible,
      dialogType,
      nurseOptions,
      traineeOptions,
      supervisorOptions,
      rules,
      showAddDialog,
      handleEdit,
      handleDelete,
      handleSubmit
    }
  }
}
</script>

<style scoped>
.workforce-container {
  padding: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
